package lab3;

import java.util.HashMap;
import java.util.Scanner;

public class AreaGrid {
	int gridSize;
	char[][] grid, gridTemp;
	HashMap<Character, Integer> areaSizes;
	char current;
	int counter;
	
	public AreaGrid() {
		super();
		gridSize=4;
		grid=new char[gridSize][gridSize];
		areaSizes = new HashMap<Character, Integer>();
		current=0;
		counter=0;
		enterGrid();
		gridTemp=grid.clone();
		checkAreaSizes();
		
	}
	
	private void enterGrid()
	{
		Scanner sc = new Scanner(System.in);
		for (int i = 0; i<gridSize; i++)
			for (int j = 0; j<gridSize; j++)
			{
				grid[i][j]=sc.next().charAt(0);
			}
	}
	
	private void checkAreaSizes()
	{
		for (int i = 0; i < gridSize; i++)
		{
			for (int j = 0; j < gridSize; j++)
			{
				counter=0;
				current=gridTemp[i][j];
				if (current>='A'&&current<='Z')	
				{	
					check (i, j);
					if (!(areaSizes.containsKey(current)))
					{
						areaSizes.put(current,counter);
					}
					else if (areaSizes.get(current)<counter)
					{
						areaSizes.put(current,counter);
					}
				}
			}
		}
	}
	
	private void check (int x, int y)
	{
		if (gridTemp[x][y]==current)
		{
			counter++;
			gridTemp[x][y]=0;
			if (x>0) check(x-1, y);
			if (x<gridSize-1) check(x+1, y);
			if (y>0) check(x, y-1);
			if (y<gridSize-1) check(x, y+1);
		}
	}
	
	private void displayValues()
	{
		for (char key : areaSizes.keySet())
		{
			System.out.println("The larges area of " + key + " has a size of " + areaSizes.get(key)+".");
		}
	}
	
	public static void main(String[] args) {
		AreaGrid grid = new AreaGrid();
		grid.displayValues();
	}

}
